package com.example.a2209_shop.utils

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.Observer

/**
 * 解决liveData数据倒灌问题
 */
class SingleLiveData2<T>:MutableLiveData<T>() {
    private var pending = false
    override fun observe(owner: LifecycleOwner, observer: Observer<in T>) {
        super.observe(owner, Observer<T>{t->
            if(!pending){
                observer.onChanged(t)
            }
        })
    }

    override fun setValue(value: T) {
        pending = true
        super.setValue(value)
    }

    override fun postValue(value: T) {
        pending = true
        super.postValue(value)
    }
}